<!DOCTYPE HTML>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  	<style type="text/css">
  	html,body{
  		font-family: Arial;
  	}
  	</style>
    <script src="js/jquery-2.1.1.min.js"></script>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
    <script type="text/javascript">
    file="movies.JSON";
    token = "ccf4ea1073d17b7640fc146a324d8b2a";
    $(document).ready(function(){
      $("#loadJson").click(function(){
         if(confirm("Seguro de cargar json?")){
           $.getJSON(file)
          .done(function(data){
             loadMovie(0,data);
          });
        }
      });
    });

    function loadMovie(a, movies){
      var index = a;
      if(index<movies.length){
        var videoQuality = new Array();
        var video = movies[index].video1; 
        video = video.toLowerCase();
        if(video!=""){
          if(video=="dvdrip") videoQuality[0] = 1;
          if(video=="720p") videoQuality[0] = 2;
          if(video=="1080p") videoQuality[0] = 3;
          if(video=="3d") videoQuality[0] = 4;
          if(video=="4k") videoQuality[0] = 5;
        }

        video = movies[index].video2; 
        video = video.toLowerCase();
        if(video!=""){
          if(video=="dvdrip") videoQuality[1] = 1;
          if(video=="720p") videoQuality[1] = 2;
          if(video=="1080p") videoQuality[1] = 3;
          if(video=="3d") videoQuality[1] = 4;
          if(video=="4k") videoQuality[1] = 5;
        }

        var audioLanguage = new Array();
        var audio = movies[index].audio1;
        audio = audio.toLowerCase();
        if(audio!=""){
          if(audio=="latino") audioLanguage[0] = 1;
          if(audio=="español latino") audioLanguage[0] = 1;
          if(audio=="ingles") audioLanguage[0] = 2;
          if(audio=="inglés") audioLanguage[0] = 2;
        }
        audio = movies[index].audio2;
        audio = audio.toLowerCase();
        if(audio!=""){
          if(audio=="latino") audioLanguage[1] = 1;
          if(audio=="español latino") audioLanguage[1] = 1;
          if(audio=="ingles") audioLanguage[1] = 2;
          if(audio=="inglés") audioLanguage[1] = 2;
        }        

        var idMpaa;
        if(movies[index].mpaa =="G") idMpaa = 1;
        if(movies[index].mpaa =="PG") idMpaa = 2;
        if(movies[index].mpaa =="PG-13") idMpaa = 3;
        if(movies[index].mpaa =="R") idMpaa = 4;
        if(movies[index].mpaa =="NC-17") idMpaa = 5;

        var bufferGenre =new Array();
        var buffer = movies[index].genre;
        for(var i=0; i<buffer.length; i++){
          if(buffer[i].toLowerCase()=="acción") bufferGenre[bufferGenre.length] = 1;
          if(buffer[i].toLowerCase()=="aventura") bufferGenre[bufferGenre.length] = 2;
          if(buffer[i].toLowerCase()=="Drama") bufferGenre[bufferGenre.length] = 11;
          if(buffer[i].toLowerCase()=="comedia") bufferGenre[bufferGenre.length] = 8;
          if(buffer[i].toLowerCase()=="familiar") bufferGenre[bufferGenre.length] = 12;
          if(buffer[i].toLowerCase()=="Fantasia") bufferGenre[bufferGenre.length] = 13;
        }
        console.log(bufferGenre);
        var data1 ={
          token : token,
          title : movies[index].title,
          originalTitle : movies[index].originalTitle,
          duration : movies[index].duration,
          releaseDate : movies[index].date,
          imdbRating : movies[index].imdbRating,
          imdbCode : movies[index].imdbCode,
          idMpaa: idMpaa,
          youtubeCode : movies[index].youTube,
          image : movies[index].image,
          sinopsis : movies[index].sinopsis,
          videoQuality:videoQuality,
          audioLanguage:audioLanguage,
          genre:bufferGenre
        }
        //alert(token+" - "+title+" - "+originalTitle+" - "+duration+" - "+releaseDate+" - "+imdbRating+" - "+imdbCode+" - "+youtubeCode+" - "+image+" - "+sinopsis+" - "+videoQuality+" - "+audioLanguage+" - "+genre);
        $.post("setMovie.php",data1)
        .done(function(data){
          var resp = jQuery.parseJSON(data);
          $("#divConsole").html($("#divConsole").html()+"<br><br>"+resp.gls);
          loadMovie(index+1, movies);
        });
      }
    }
    </script>
  </head>
  <body>
    <div id="divConsole" style="width:600px; height:700px; position:absolute; right:0; top:0; border: 1px solid #F00;"></div>
  	<form action="http://www.colegiozanandrea.cl/mv/setMovie.php" method="POST">
      <input type="hidden" id="token" name="token" value="ccf4ea1073d17b7640fc146a324d8b2a">
  		Titulo<br/>
  		<input type="text" id="title" name="title" required><br/>
  		Titulo original<br/>
  		<input type="text" id="originalTitle" name="originalTitle"><br/>
  		Duración<br/>
  		<input type="number" id="duration" name="duration" required><br/>
  		Fecha de estreno<br/>
  		<input type="date" id="releaseDate" name="releaseDate" required><br/>
  		Restricción MPAA<br/>
  		<select id="idMpaa" name="idMpaa">
  			<option value="1">G - Apta para todo público</option>
  			<option value="2">PG - Apta para mayores de 10 años</option>
  			<option value="3">PG-13 - Apta para mayores de 13 años</option>
  			<option value="4">R - Apta para mayores de 17 años acompañados</option>
  			<option value="5">NC-17 - Apta mayores de 18 años</option>
  		</select><br/>
  		Evaluación IMDB<br/>
  		<input type="text" id="imdbRating" name="imdbRating" required><br/>
  		Codigo IMDB<br/>
  		<input type="text" id="imdbCode" name="imdbCode" required><br/>
  		Codigo trailer Youtube<br/>
  		<input type="text" id="yotubeCode" name="youtubeCode" required><br/>
  		Nombre archivo poster<br/>
  		<input type="text" id="image" name="image" required><br/>
  		Sinopsis<br/>
  		<textarea id="sinopsis" name="sinopsis"></textarea><br/>
  		Calidad de video<br/>
  		<select id="videoQuality" name="videoQuality[]" multiple>
  			<option value="1">DvdRip</option>
  			<option value="2">720p</option>
  			<option value="3">1080p</option>
  			<option value="4">3D</option>
  			<option value="5">4K</option>
  		</select><br/>
  		Lenguaje del audio<br/>
  		<select id="audioLanguage" name="audioLanguage[]" multiple>
  			<option value="1">Latino</option>
  			<option value="2">Inglés</option>
  			<option value="3">Castellano</option>
  			<option value="4">Japones</option>
  			<option value="5">Coreano</option>
  			<option value="6">Francés</option>
  			<option value="7">Portugués</option>
  			<option value="8">Alemán</option>
  		</select><br/>
  		Género<br/>
  		<select id="genre" name="genre[]" multiple>
  			<option value="1">Acción</option>
        <option value="20">Animación</option>
  			<option value="2">Aventura</option>
  			<option value="3">Bíblica</option>
  			<option value="4">Belica</option>
  			<option value="5">Biografica</option>
  			<option value="6">Ciencia Ficción</option>
  			<option value="7">Catastrofe</option>
  			<option value="8">Comedia</option>
			  <option value="9">Deportiva</option>
			  <option value="10">Documentales</option>
			  <option value="11">Drama</option>
			  <option value="12">Familiar</option>
			  <option value="13">Fantastica</option>
			  <option value="14">Historica</option>
			  <option value="15">Infantil</option>
			  <option value="16">Misterio</option>
			  <option value="17">Musical</option>
			  <option value="18">Romantica</option>
			  <option value="19">Terror</option>
  		</select><br/>
  		<input type="submit">
      <input type="reset">
  	 <input type="button" value="Cargar JSON" id="loadJson">
    </form>
  </body>
</html>